package com.ruoyi.app.service.impl;

import java.beans.Transient;
import java.util.Date;
import java.util.List;

import com.ruoyi.app.domain.AppCaseInfo;
import com.ruoyi.app.domain.AppFile;
import com.ruoyi.app.domain.AppOperationLog;
import com.ruoyi.app.mapper.AppCaseInfoMapper;
import com.ruoyi.app.mapper.AppFileMapper;
import com.ruoyi.app.mapper.AppOperationLogMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.AppCaseReceiptMapper;
import com.ruoyi.app.domain.AppCaseReceipt;
import com.ruoyi.app.service.IAppCaseReceiptService;
import com.ruoyi.common.core.text.Convert;

/**
 * 案件回执Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-12-12
 */
@Service
public class AppCaseReceiptServiceImpl implements IAppCaseReceiptService 
{
    @Autowired
    private AppCaseReceiptMapper appCaseReceiptMapper;
    @Autowired
    private AppFileMapper appFileMapper;
    @Autowired
    private AppOperationLogMapper appOperationLogMapper;
    @Autowired
    private AppCaseInfoMapper appCaseInfoMapper;

    /**
     * 查询案件回执
     * 
     * @param id 案件回执ID
     * @return 案件回执
     */
    @Override
    public AppCaseReceipt selectAppCaseReceiptById(Long id)
    {
        return appCaseReceiptMapper.selectAppCaseReceiptById(id);
    }

    /**
     * 查询案件回执列表
     * 
     * @param appCaseReceipt 案件回执
     * @return 案件回执
     */
    @Override
    public List<AppCaseReceipt> selectAppCaseReceiptList(AppCaseReceipt appCaseReceipt)
    {
        return appCaseReceiptMapper.selectAppCaseReceiptList(appCaseReceipt);
    }

    /**
     * 新增案件回执
     * 
     * @param appCaseReceipt 案件回执
     * @return 结果
     */
    @Transient
    @Override
    public int insertAppCaseReceipt(AppCaseReceipt appCaseReceipt) {

        appCaseReceiptMapper.insertAppCaseReceipt(appCaseReceipt);

        //更新图片信息
        if(StringUtils.isNotEmpty(appCaseReceipt.getAppFileIds())){
            List<Long> appFileIds = appCaseReceipt.getAppFileIds();
            for(Long fielId :appFileIds){
                AppFile appFile = appFileMapper.selectAppFileById(fielId);
                appFile.setSourceId(appCaseReceipt.getId());
                appFileMapper.updateAppFile(appFile);
            }
        }

        //更新日志信息
        AppOperationLog appOperationLog = new AppOperationLog();
        appOperationLog.setOperaType("addReceip");
        appOperationLog.setOperaContent(appCaseReceipt.getOperator()+"上传回执");
        appOperationLog.setOperaTime(new Date());
        appOperationLog.setSourceId(appCaseReceipt.getId());
        appOperationLog.setState(0);
        appOperationLog.setUserId(appCaseReceipt.getOperatorId());
        appOperationLog.setUserName(appCaseReceipt.getOperator());
        appOperationLogMapper.insertAppOperationLog(appOperationLog);

        //更新案件状态
        AppCaseInfo appCaseInfo = appCaseInfoMapper.selectAppCaseInfoById(appCaseReceipt.getCaseId());
        appCaseInfo.setState("1");
        appCaseInfoMapper.updateAppCaseInfo(appCaseInfo);

        return 1;

    }

    /**
     * 修改案件回执
     * 
     * @param appCaseReceipt 案件回执
     * @return 结果
     */
    @Override
    public int updateAppCaseReceipt(AppCaseReceipt appCaseReceipt)
    {
        return appCaseReceiptMapper.updateAppCaseReceipt(appCaseReceipt);
    }

    /**
     * 删除案件回执对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAppCaseReceiptByIds(String ids)
    {
        return appCaseReceiptMapper.deleteAppCaseReceiptByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除案件回执信息
     * 
     * @param id 案件回执ID
     * @return 结果
     */
    @Override
    public int deleteAppCaseReceiptById(Long id)
    {
        return appCaseReceiptMapper.deleteAppCaseReceiptById(id);
    }
}
